﻿using OrderSystem.Common;
using OrderSystem.DataAccess;
using OrderSystem.Templet;
using System;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace OrderSystem
{
    public partial class FormAddInvoice : FormTemplet
    {

        private int _nCustomerID = -1;

        private int _nProjectID = -1;

        public FormAddInvoice()
        {
            this.InitializeComponent();
        }

        private void btnSelectCustomer_Click(object sender, EventArgs e)
        {
            CustomerData customerData = FormSelectCustomer.ShowBox();
            if (customerData != null)
            {
                this._nCustomerID = customerData.CustomerID;
                this.txtCustomerName.Text = customerData.CompanyName + "(" + customerData.Linkman + ")";
                this._nProjectID = -1;
                this.txtProjectName.Text = "";
                this.btnSelectProject.Focus();
            }
        }

        private void btnSelectProject_Click(object sender, EventArgs e)
        {
            if (this.txtCustomerName.Text.Trim() == "" || this._nCustomerID == -1)
            {
                MessageBox.Show("请选择客户", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.btnSelectCustomer.Focus();
            }
            else
            {
                ProjectData projectData = FormSelectProject.ShowBox(this._nCustomerID);
                if (projectData != null)
                {
                    this._nProjectID = projectData.ProjectID;
                    this.txtProjectName.Text = projectData.ProjectName + "(" + projectData.Linkman + ")";
                    this.txtRemark.Focus();
                }
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (this.txtCustomerName.Text.Trim() == "" || this._nCustomerID == -1)
            {
                MessageBox.Show("请选择客户", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.btnSelectCustomer.Focus();
            }
            else if (this.txtProjectName.Text.Trim() == "" || this._nProjectID == -1)
            {
                MessageBox.Show("请选择对此客户的计划项目", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.btnSelectProject.Focus();
            }
            else if (this.txtRemark.Text.Trim().Length > 64)
            {
                MessageBox.Show("备注字符太长", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.txtRemark.Focus();
            }
            else
            {
                Invoices invoices = new Invoices();
                string text = null;
                try
                {
                    text = invoices.InsertInvoice("", Convert.ToInt32(this._nProjectID), this.txtRemark.Text.Trim(), LoginInfo.OperatorID);
                }
                catch (SqlException ex)
                {
                    MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                }
                catch (Exception ex2)
                {
                    MessageBox.Show(ex2.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                invoices.Dispose();
                if (text != null)
                {
                    MessageBox.Show("出货单创建成功！", "Success", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    new FormInvoiceInfo(text)
                    {
                        MdiParent = base.MdiParent
                    }.Show();
                    base.Close();
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            base.Close();
        }
    }
}
